<?php
	///////////////////////////////////////////////////////////////////////////////////////////////////
	//	SUBMITTED TIME
	//	WRITTEN BY: TRAVIS THORNTON
	//	12-07-2009
	//
	//	What this page does:
	//	1) Shows submitted timesheets for a manager's employees
	//	2) Allows approval and viewing of the timesheet
	//
	////////////////////////////////////////////////////////////////////////////////////////////////////
		
	session_start();
	require_once 'php_includes.php';
	require_once 'time_functions.php';

	//Make sure logged in
	checkLogin();

	//make sure it is a manager logged on
	checkManager();

	//handle approvals
	$php_message = "";

	//if approving
	if($_POST['action'] == "approve")
	{ 
		//change the status from 1 to 2
		$sql = 'UPDATE `' . $dbname . '`.`PayPeriod` SET `PeriodStatus` = \'2\' WHERE EmployeeID = \'' . $_POST['usertime'] . '\' AND PeriodStart = \''. $_POST['period'] .'\' AND `PeriodStatus` = \'1\'';
		sqlConnect();
		$sqlresult = sqlQuery($sql);
		sqlExit();
		
		//set message
		$php_message = "Timesheet approved.";	
	}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Payroll and Timesheet Management Website</title>
    <link rel="stylesheet" type="text/css" href="styles.css"/>
  </head>
  <body>
	<?php writeHeader(); ?>
	<p>
	<span class="pageheader">Submitted employee timesheets requiring approval</span>
	</p><br />
	<?php
	//if message 
	if($php_message != "")
	{
		echo "<p class=\"php_message\">";
		echo $php_message;
		echo "</p>";
	}
	?>
	
	<table class="full">
	<tr>
	<th></th>
	<th></th>
	<th>Employee</th>
	<th>Period</th>
	<th>Normal Time</th>
	<th>Overtime</th>
	<th>Double Time</th>
	<th>Bonus Time</th>
	</tr>
	
	<?php
	//get all the pending approval timesheets
	sqlConnect();
	$sql = "SELECT PeriodStart, PeriodEnd, Employee.EmployeeID, EmployeeFName, EmployeeLName FROM PayPeriod, Employee WHERE Employee.EmployeeID = PayPeriod.EmployeeID AND PeriodStatus = 1 AND ManagerID = '" . $_SESSION['tmsUserID'] . "' ORDER BY PeriodStart";
	$results = sqlQuery($sql);
	
	//get numm rowa
	$num_rows = mysql_num_rows($results);
	sqlExit();
	
	//if none, show no results
	if($num_rows == 0)
	{
		echo "<tr>\n";
		echo "<td class=\"record\" colspan=\"8\">No Records Available</td>";
		echo "</tr>";	
	}
	else//otherwise
	{
		//fill out table
		for($i = 0; $i < $num_rows; ++$i)
		{
			$data = mysql_fetch_row($results);
			echo "<tr>\n";
			echo "<td class=\"view\"><form action=\"submittedtime.php\" method=\"post\"><div>";
			
			//form data for the timesheet
			echo "<input type=\"hidden\" name=\"action\"value=\"approve\" />";
			echo "<input type=\"hidden\" name=\"usertime\"value=\"" . $data[2] . "\" />";
			echo "<input type=\"hidden\" name=\"period\"value=\"" . $data[0] . "\" />";
			
			echo "<input type=\"submit\" value=\"Approve\" class=\"button\" /></div></form></td>\n";
			
			
			echo "<td class=\"view\"><form action=\"timesheet.php\" method=\"post\"><div>";
			
			//form data for the timesheet
			echo "<input type=\"hidden\" name=\"usertime\"value=\"" . $data[2] . "\" />";
			echo "<input type=\"hidden\" name=\"period\"value=\"" . $data[0] . "\" />";
			echo "<input type=\"submit\" value=\"View\" class=\"button\" /></div></form></td>\n";
			
			echo "<td class=\"record\">";
			echo $data[3] . " " . $data[4] . "(". $data[2].")";
			echo "</td>\n";
			
			echo "<td class=\"record\">";
			//Format the mysql date correctly
			//10/18/2009 - 10/31/2009
			$date1 = explode("-", $data[0]);
			$date2 = explode("-", $data[1]);
			echo $date1[1] . "/" . $date1[2] . "/" . $date1[0] . " - " . $date2[1] . "/" . $date2[2] . "/" . $date2[0];
			
			echo "</td>\n";
			
			//Get total time for this period
			generate2WeekTotals($data[2], $data[0]);//generate the total time by passing in information to extract the row
			
			echo "<td class=\"record\">";
			echo getHours();
			echo "</td>\n";
			echo "<td class=\"record\">";
			echo getOTHours();
			echo "</td>\n";
			echo "<td class=\"record\">";
			echo getDoubleHours();
			echo "</td>\n";
			echo "<td class=\"record\">";
			echo getBonusHours();
			echo "</td>\n";
			
			resetGlobalTime();//reset the globals for the next row
			
			echo "</tr>\n";
		}
	}
	?>
	</table>
  </body>
</html>